$(function(){

     $.ajax({
            url:url.searchUserOrderList,
            type:"post",
            dataType:"json",
            contentType: "application/json",
            //beforeSend:提交请求前执行
            beforeSend: function (request) {
                request.setRequestHeader("token",localStorage.getItem("token"));
            },
            data:JSON.stringify({
                "page":1,
                "length":20
            }),
            success: function (resp) {
                if(resp.code == 0){
                    // console.log(resp)
                    let list = resp.list
                    let temp = ''
                    for(let one of list){
                        let status = one.status
                        if(status==1){
                            one.status = "未付款"
                        }else if(status==2){
                            one.status = "已付款"
                        }else if(status==3){
                            one.status = "已发货"
                        }else if(status==4){
                            one.status = "已签收"
                        }
                        //截取订单流水号的的前12个字符
                        one.code = one.code.substr(0,12)+"......"
                        let button = ''
                        if(one.status=="未付款"){
                            button = '<input type="button" value="支付" class="pay-btn" />'
                        }
                        temp += `
                            <div class="order" data-order-id = "${one.id}">
                                <div class="line-1">
                                    <span>订单号：${one.code}</span>
                                    <span>${one.status}</span>
                                </div>
                                <div class="line-2">
                                    <div>假设这里是商品简要信息</div>
                                </div>
                                <div class="line-3">
                                    <span>金额：${one.amount}元</span>
                                     ${button}
                                </div>
                        </div>
                        `
                    }
                    //更新加载订单页数据
                    $(".order-list").append(temp)

                    //支付按钮点击事件
                    $(".pay-btn").click(function () {
                        let orderId = $(this).parents(".order").data("order-id")
                        //支付宝付款
//                        $.ajax({
//                            url:url.zfb.appPayOrder,
//                            type:"post",
//                            dataType: "json",
//                            contentType: "application/json",
//                            beforeSend: function (request) {
//                                request.setRequestHeader("token",localStorage.getItem("token"));
//                            },
//                            data:JSON.stringify({
//                                "orderId":orderId
//                            }),
//                            success:function (resp) {
//                                let orderString  = resp.orderString
//                                console.log(orderString)
//                                window.order.pay(orderString,localStorage.getItem("token"),orderId)
//                            }
//                        })

                        //银联付款
                        $.ajax({
                            url:url.unionPay.wapPayOrder,
                            type:"post",
                            dataType: "json",
                            contentType: "application/json",
                            beforeSend: function (request) {
                                request.setRequestHeader("token",localStorage.getItem("token"));
                            },
                            data:JSON.stringify({
                                "orderId":orderId
                            }),
                            success:function (resp) {
                                let code  = resp.code
//                                $("#temp").text(code)
                                window.order.wapPay(code)
                            }
                        })
                    })
                }
            }
        })
    
})